import { action, observable } from 'mobx';
import * as Api from '../apis/superviseApi';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';

const { getLabel } = WeaLocaleProvider;
const { TableStore } = WeaTableNew;

export default class SuperviseStore {
  @observable workflowid = '';
  @observable operationType = '0';
  @observable requestid = ''
  @observable superviseVisible = false;
  @observable superviseForm = new WeaForm();// 督办
  @observable superviseconditions = [];
  @observable urgeForm = new WeaForm();// 催办
  @observable urgeVisible = false;
  @observable delayRequestForm = new WeaForm();
  @observable delayRequestVisible = false;
  @observable superviseid = '';
  @observable detailForm = new WeaForm();
  @observable detailTabs = []
  @observable detailSelectKey = '0';
  @observable tableStore1 = new TableStore();
  @observable tableStore2 = new TableStore();
  @observable tableStore3 = new TableStore();
  @observable detailDialogVisible = false;
  @observable remindSetingVisible = false;
  @observable remindSetingForm = new WeaForm();

  @action.bound
  onSupervise() {
    const params = {
      workflowid: this.workflowid,
      operationType: this.operationType,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.formConditions) {
        this.superviseconditions = [];
        this.superviseForm = new WeaForm();
        this.superviseconditions = data.formConditions;
        this.superviseForm.initFormFields(data.formConditions);
        this.remindSetingForm.setCondition(data.setConditions);
        this.superviseVisible = true;
      }
    });
  }

  @action.bound
  reminder() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.conditions) {
        this.urgeForm = new WeaForm();
        this.urgeForm.setCondition(data.conditions);
        this.urgeVisible = true;
      }
    });
  }

  @action.bound
  checkDetails() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      this.detailForm = new WeaForm();
      this.tableStore1 = new TableStore();
      this.tableStore2 = new TableStore();
      this.tableStore3 = new TableStore();
      this.detailForm.setCondition(data.conditions);
      this.detailTabs = data.topTablist;
      this.tableStore1.getDatas(data.datalists.remindTableKey);
      this.tableStore2.getDatas(data.datalists.urgeTableKey);
      this.tableStore3.getDatas(data.datalists.delayTableKey);
      this.detailDialogVisible = true;
    });
  }

  @action.bound
  cancelReminder(getTableDatas, getTabData) {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('129248', '取消成功'));
        getTableDatas();
        getTabData();
      }
    });
  }

  @action.bound
  delayDate(getTableDatas) {
    const params = {
      workflowid: this.workflowid,
      operationType: this.operationType,
      requestid: this.requestid,
      superviseid: this.superviseid,
      ...this.delayRequestForm.getFormParams(),
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('517320', '延期处理申请提交成功'));
        this.delayRequestVisible = false;
        getTableDatas();
      }
    });
  }

  @action.bound
  superviseHide() {
    this.superviseVisible = false;
    this.workflowid = '';
    this.operationType = 0;
    this.requestid = '';
  }

  @action.bound
  saveSupervise(getTableDatas, getTabData) {
    const formParams = this.superviseForm.getFormParams();
    const remindSetingFormParams = this.remindSetingForm.getFormParams();
    const params = {
      ...formParams,
      ...remindSetingFormParams,
      requestid: this.requestid,
      operationType: this.operationType,
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        this.superviseHide();
        getTableDatas();
        getTabData();
        message.success(getLabel('387108', '督办成功'));
      }
    });
  }

  @action.bound
  saveUrge() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
      ...this.urgeForm.getFormParams(),
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        this.urgeHide();
        message.success(getLabel('517321', '催办成功'));
      }
    });
  }

  @action.bound
  setMobxState(params = {}) {
    Object.keys(params).map((key) => {
      this[key] = params[key];
    });
  }

  @action.bound
  urgeHide() {
    this.urgeVisible = false;
  }

  @action.bound
  validateForm(form, callBack, getTableDatas = null, getTabData = null) {
    form.validateForm().then((f) => {
      if (f.isValid) {
        callBack(getTableDatas, getTabData);
      } else {
        f.showErrors();
      }
    });
  }

  @action.bound
  validateRemindSetingForm() {
    const superviseParams = this.superviseForm.getFormParams();
    const remindSetingParams = this.remindSetingForm.getFormParams();
    if (superviseParams.superviseRemindType.includes('0')) {
      if (remindSetingParams.beforeRemindType === '' || remindSetingParams.beforeRemindNum === '' || remindSetingParams.beforeRemindTime === '') {
        message.warn(getLabel('508377', '有必填项没有填写'));
        return;
      }
    }
    if (superviseParams.superviseRemindType.includes('1')) {
      if (remindSetingParams.afterRemindType === '' || remindSetingParams.afterRemindNum === '' || remindSetingParams.afterRemindTime === '') {
        message.warn(getLabel('508377', '有必填项没有填写'));
        return;
      }
    }
    this.remindSetingHide();
  }

  @action.bound
  getDelayDateCondtion() {
    const params = {
      operationType: this.operationType,
      requestid: this.requestid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.conditions) {
        this.delayRequestForm = new WeaForm();
        this.delayRequestForm.setCondition(data.conditions);
        this.delayRequestVisible = true;
      }
    });
  }

  @action.bound
  checkChange(type) {
    const superviseRemindType = this.superviseForm.getFormParams().superviseRemindType;
    let values = '';
    if (type == 'pre') {
      values = superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '1' :
        !superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '0,1' :
          superviseRemindType.includes('0') && !superviseRemindType.includes('1') ? '' : '0';
    } else {
      values = superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '0' :
        !superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '' :
          superviseRemindType.includes('0') && !superviseRemindType.includes('1') ? '0,1' : '1';
    }
    this.superviseForm.updateFields({ superviseRemindType: { value: values } });
  }

  @action.bound
  remindSetingHide() {
    this.remindSetingVisible = false;
  }
}
